package com.simba.server.components;



import com.simba.common.log.Log4jLogger;
import com.simba.common.log.LogTask;
import com.simba.common.log.LoggerExecutor;

import com.simba.server.commands.impl.fetch.FetchBasicConfigCmd;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

public class ConfigManager {
    private static class ConfigManagerHolder
    {
        private static ConfigManager INSTANCE = new ConfigManager();
    }

    protected final static Logger logger = Logger.getLogger(ConfigManager.class);

    public static ConfigManager getInstance()
    {
        return ConfigManager.ConfigManagerHolder.INSTANCE;
    }

    private ConfigManager()
    {

    }

    public void loadBasicConfig()
    {
        try
        {
            HashMap<String, String> configMap = new HashMap<>();
            CommandExecutor.executorSqlCommand(FetchBasicConfigCmd.NAME, configMap,false);

            if (configMap.size() > 0)
            {
                this.setBasicConfigs(configMap);
            }
        }
        catch (Exception ex)
        {
            LoggerExecutor.getInstance()
                    .execute(new LogTask(new Log4jLogger(MachineParams.logger),
                            "[SorterParameter.initSortParams] init sorter params error: " + ex,
                            LogTask.ERROR));
        }
    }

    private int getIntValue(Map<String, String> configMap, String key, int defaultValue)
    {
        String value = configMap.get(key);
        if (value != null)
        {
            try
            {
                return Integer.parseInt(value);
            }
            catch (Exception e)
            {
                return defaultValue;
            }

        }else
        {
            return defaultValue;
        }

    }

    private boolean getBooleanValue(Map<String, String> configMap, String key, boolean defaultValue)
    {
        String value = configMap.get(key);
        if (value != null)
        {
            try
            {
                return Boolean.parseBoolean(value);
            }
            catch (Exception e)
            {
                return defaultValue;
            }

        }else
        {
            return defaultValue;
        }

    }
    private String getStringValue(Map<String, String> configMap, String key, String defaultValue)
    {
        String value = configMap.get(key);
        if (value != null)
        {
           return value;

        }else
        {
            return defaultValue;
        }

    }

    private void setBasicConfigs(HashMap<String, String> configMap)
    {
        MachineParams.getInstance().setRunStatus(this.getStringValue(configMap, "runStatus", "none"));
        MachineParams.getInstance().setServerId(this.getStringValue(configMap, "serverId", "1"));
        MachineParams.getInstance().setSlotNum(this.getIntValue(configMap, "slotNum", 300));
        MachineParams.getInstance().setDeviceId(this.getStringValue(configMap, "deviceId", "none"));
        MachineParams.getInstance().setWorkshopCode(this.getStringValue(configMap, "workshopCode", "none"));
        MachineParams.getInstance().setCenterSituation(this.getStringValue(configMap, "centerSituation", "none"));

        SorterParams.getInstance().setLogSaveDays(this.getIntValue(configMap, "logSaveDays", 30));
        SorterParams.getInstance().setCheckBarcode(this.getBooleanValue(configMap, "checkBarcode", true));
        SorterParams.getInstance().setUploadNum(this.getIntValue(configMap, "uploadNum", 3));
        SorterParams.getInstance().setMultiCodeCheck(this.getBooleanValue(configMap, "multiCodeCheck", false));
        SorterParams.getInstance().setRestorePacketOffDays(this.getIntValue(configMap, "restorePacketOffDays", 3));
        SorterParams.getInstance().setRestoreSlotPackDays(this.getIntValue(configMap, "restoreSlotPackDays", 5));
        SorterParams.getInstance().setRestoreBoxDays(this.getIntValue(configMap, "restoreBoxDays", 5));
        SorterParams.getInstance().setDeleteDataTime(this.getIntValue(configMap, "deleteDataTime", 12));
        SorterParams.getInstance().setSyncDataTime(this.getIntValue(configMap, "syncDataTime", 5));
    }
}
